Export Scala.js APIs to JavaScript
Toplevel method export]
code:scala
object HelloWorld2 {
@JSExportTopLevel("foo")
def foo(x: Int): Int = x + 1
}
code:javascript
const x = foo(5)
code:scala
module class sample.HelloWorld2$ extends java.lang.Object {
def foo;I;I(x: int): int = {
}
constructor def <init>;V() {
this.java.lang.Object::<init>;V();
mod:sample.HelloWorld2$<-this
}
val prep0: int = arg.asInstanceOfint; mod:sample.HelloWorld2$.foo;I;I(prep0)
}
}
class sample.HelloWorld2 extends java.lang.Object {
static def foo;I;I(x: int): int = {
mod:sample.HelloWorld2$.foo;I;I(x)
}
}
module class が見つかる
(type ... (struct (vtable fields) )
vtable には constructor は含めない
code:wasm
(global sampleHelloWorld2$ (type sample.HelloWorld2$))
(func (export foo) (param i32) (result i32)
global.get sample.HelloWorld2$
)
code:scala
MethodDef(
org.scalajs.ir.Trees$MemberFlags@0,
MethodIdent(MethodName<foo;I;I>),
NoOriginalName,
List(ParamDef(LocalIdent(LocalName<x>),NoOriginalName,IntType,false)),
IntType,
Some(BinaryOp(8,VarRef(LocalIdent(LocalName<x>)),IntLiteral(1))))
MethodDef(
org.scalajs.ir.Trees$MemberFlags@4,
MethodIdent(MethodName<<init>;V>),
NoOriginalName,
List(),
NoType,
Some(
Block(
ApplyStatically(
org.scalajs.ir.Trees$ApplyFlags@2,
This(),
ClassName<java.lang.Object>,
MethodIdent(MethodName<<init>;V>),
List()
),
StoreModule(ClassName<sample.HelloWorld2$>,This())
)
)
)
TopLevelMethodExportDef(
main,
JSMethodDef(
org.scalajs.ir.Trees$MemberFlags@1,
StringLiteral(foo),
List(ParamDef(LocalIdent(LocalName<arg>),NoOriginalName,AnyType,false)),
None,
Block(
VarDef(
LocalIdent(LocalName<prep0>),
NoOriginalName,
IntType,
false,
AsInstanceOf(VarRef(LocalIdent(LocalName<arg>)),IntType)
),
Apply(
org.scalajs.ir.Trees$ApplyFlags@0,
LoadModule(ClassName<sample.HelloWorld2$>),
MethodIdent(MethodName<foo;I;I>),
List(VarRef(LocalIdent(LocalName<prep0>)))
)
)
)
)
code:scala
MethodDef(
org.scalajs.ir.Trees$MemberFlags@1,
MethodIdent(MethodName<foo;I;I>),
NoOriginalName,
List(
ParamDef(LocalIdent(LocalName<x>),NoOriginalName,IntType,false)
),
IntType,
Some(
Apply(
org.scalajs.ir.Trees$ApplyFlags@0,
LoadModule(ClassName<sample.HelloWorld2$>),
MethodIdent(MethodName<foo;I;I>),
List(VarRef(LocalIdent(LocalName<x>)))
)
)
)
Wasm の export メソッドのインターフェースに使えるのは external, primitive, string and function types だけであるべきだよね?
Any じゃなくて ParamDef(LocalIdent(LocalName<arg>),NoOriginalName,IntType,false) になって欲しい
restParam って何???
module class の constructor はは明示的に呼ばれるわけではなく、module class のメソッドが最初に呼び出されるときに、その実行の前に init が実行されるという理解でOK?
hr.icon
Export toplevel
code:scala
package sample
import scala.scalajs.js.annotation._
@JSExportTopLevel("HelloWorld")
object HelloWorld {
@JSExport
def sayHello(): Unit = { }
}
code:scala
module class sample.HelloWorld$ extends java.lang.Object {
def sayHello;V() {
// LoadModule(...)
mod:scala.Predef$.println;Ljava.lang.Object;V("Hello world!")
}
def $js$exported$meth$sayHello;Ljava.lang.Object(): any = {
mod:sample.HelloWorld$.sayHello;V();
(void 0)
}
constructor def <init>;V() {
this.java.lang.Object::<init>;V();
mod:sample.HelloWorld$<-this
}
def "sayHello"(): any = {
this.$js$exported$meth$sayHello;Ljava.lang.Object()
}
}
class sample.HelloWorld extends java.lang.Object {
static def $js$exported$meth$sayHello;Ljava.lang.Object(): any = {
mod:sample.HelloWorld$.$js$exported$meth$sayHello;Ljava.lang.Object()
}
static def sayHello;V() {
mod:sample.HelloWorld$.sayHello;V()
}
}
code:scala
MethodDef(
org.scalajs.ir.Trees$MemberFlags@0,
MethodIdent(MethodName<sayHello;V>),
NoOriginalName,
List(),
NoType,
Some(Skip())
)
MethodDef(
org.scalajs.ir.Trees$MemberFlags@0,
MethodIdent(MethodName<$js$exported$meth$sayHello;Ljava.lang.Object>),
NoOriginalName,
List(),
AnyType,
Some(
Block(
Apply(
org.scalajs.ir.Trees$ApplyFlags@0,
LoadModule(ClassName<sample.HelloWorld$>),
MethodIdent(MethodName<sayHello;V>),
List()
),
Undefined()
)
)
)
MethodDef(
org.scalajs.ir.Trees$MemberFlags@4,
MethodIdent(MethodName<<init>;V>),
NoOriginalName,
List(),
NoType,
Some(
Block(
ApplyStatically(
org.scalajs.ir.Trees$ApplyFlags@2,
This(),
ClassName<java.lang.Object>,
MethodIdent(MethodName<<init>;V>),
List()
),
StoreModule(ClassName<sample.HelloWorld$>,This())
)
)
)
code:scala
MethodDef(
org.scalajs.ir.Trees$MemberFlags@1,
MethodIdent(MethodName<$js$exported$meth$sayHello;Ljava.lang.Object>),
NoOriginalName,
List(),
AnyType,
Some(Apply(
org.scalajs.ir.Trees$ApplyFlags@0,
LoadModule(ClassName<sample.HelloWorld$>),
MethodIdent(MethodName<$js$exported$meth$sayHello;Ljava.lang.Object>),
List()
))
)
MethodDef(
org.scalajs.ir.Trees$MemberFlags@1,
MethodIdent(MethodName<sayHello;V>),
NoOriginalName,
List(),
NoType,
Some(Apply(
org.scalajs.ir.Trees$ApplyFlags@0,
LoadModule(ClassName<sample.HelloWorld$>),
MethodIdent(MethodName<sayHello;V>),
List()
))
)
なんですかこれは
hr.icon